package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 水表供应商-添加
 *
 * @author 廖大富
 */
public class MeterFactoryAdd implements ServerInterface {

    private static Logger logger = LogManager.getLogger(MeterFactoryAdd.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            MeterFactoryMapper mapper = factory.getMapper(MeterFactoryMapper.class);
            MeterFactoryBean bean = JsonUtils.objectToPojo(req.getData(), MeterFactoryBean.class);
            // 校验字段重复
            if (mapper.check("factory_name", bean.getFactoryName()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "厂家名称：" + bean.getFactoryName() + "的信息已存在。", void.class);
            }
            if (mapper.check("factory_full_name", bean.getFactoryFullName()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "厂家全称：" + bean.getFactoryFullName() + "的信息已存在。", void.class);
            }
            bean.setCreateInfo(userInfo);
            mapper.insertMeterFactory(bean);
        } catch (Exception e) {
            logger.error("水表供应商添加失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "水表供应商添加失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }
}